
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Newsss extends CI_Controller{
	public function __construct(){
		parent::__construct();
		// $this->load->helper('msg');
		$this->load->helper('url');
		// $this->load->model('Newer');
		// $this->load->model('Cate');
		$this->load->library('upload');
		$this->load->library('pagination');
		$this ->load ->database(); 
	}

// 新闻列表
	public function index()
	{

		//分页
		$this->load->library('pagination');
		$perPage =5;
		//$config['base_url'] = "http://127.0.0.1/study/htdocs/php5/ci/index.php/floor/index";
		$config['base_url'] = site_url().'admin/newsss/index';
		// 计算总记录数
		$config['total_rows'] = $this->db->count_all_results('news');
		$config['per_page'] =$perPage;
		$config['first_link'] = '第一页';
		$config['prev_link'] = '上一页';
		$config['next_link'] = '下一页';
		$config['last_link'] = '最后一页';
		$this->pagination->initialize($config);
		$links = $this->pagination->create_links();
		$offset = $this->uri->segment(3);
		 $this->db->limit($perPage,$offset);
		//查看须在后
		$this->load->model('News');
	    
		$arr=$this->News->newsall();
		
/*		$this->load->view('home/commont/head');
		$this->load->view('home/commont/left');*/
		$this->load->view('admin/news/index',['arr'=>$arr,'links'=>$links]);
	}




//新闻列表
	// public function index($offset='')
	// {
	// 	      //导入分页类
	// 	$perPage =3;          //每页显示的记录数
	// 	$config['base_url'] = site_url('Admin/news/newslist');
 // //导入分页类URL
	// 	$config['total_rows'] = $this->db->count_all('news');  //计算总记录数
	// 	// print_r($config['total_rows']);die;
 //       //自定义分页连接  
 //        $config['per_page'] =$perPage;
 //        $config['next_link'] = '下一页>'; 
	// 	$config['prev_link'] = '<上一页';  
	// 	$this->pagination->initialize($config);      //初始化分类页     
	// 	$data['link'] = $this->pagination->create_links();
	// 	$limit = $config['per_page'];
	// 	$this->db->limit($limit,$offset);	
	// 	$data['results']=$this->Newer->getnews();
	// 	$this->load->view('admin/news/list',$data);
	// }


	// 新闻添加
	public function add()
	{
		if ($this ->input ->post()) {
			// echo "string";die;
			$data['news_title'] = $this->input->post('news_title');
			$data['news_content'] = $this->input->post('news_content');
			$data['news_time'] = time();
			// 图片上传
			$config['upload_path']      = './public/uploads/';
	        $config['allowed_types']    = 'gif|jpg|png';
	        $config['max_size']     = 1000;
	        $config['max_width']        = 1024;
	        $config['max_height']       = 768;
	        // 初始化配置文件
	        $this->upload->initialize($config);
	        if ( ! $this->upload->do_upload('news_img'))
	        {
	           echo('图片上传失败');
	           header("refresh:2;url=".site_url()."/admin/newsss/add");
	        }
	        else
	        {
	            $data['news_img'] = $this->upload->data('file_name');
	    		
	        }
	        $res = $this->db->insert('news',$data);
	        if($res){
	        	echo "<script>alert('提交成功');</script>";
	        	redirect('admin/newsss/index');
	        }else{
	        	echo('文章添加失败');
	           header("refresh:2;url=".site_url()."/admin/newsss/add");
	        }
		}else{
			// echo "123";
			$this->load->view('admin/news/add');
		}
	}
	
	//新闻删除
	public function del()
	{



		$id = $this->input->get('id');
		$res = $this->db->delete('news',['news_id'=>$id]);
		if($res){
			
	        redirect('admin/newsss/index');
		}else{
			echo('删除失败');
	        header("refresh:2;url=".site_url()."/admin/newsss/index");
		}
	}

	//修改默认
	public function update()
	{
		if ($this ->input ->post()) {
			// echo "string";die;
			$data['news_title'] = $this->input->post('news_title');
			$data['news_content'] = $this->input->post('news_content');
			$data['news_time'] = time();
			$news_id=$this ->input ->post('news_id');
			// 图片上传
			$config['upload_path']      = './public/uploads/';
	        $config['allowed_types']    = 'gif|jpg|png';
	        $config['max_size']     = 1000;
	        $config['max_width']        = 1024;
	        $config['max_height']       = 768;
	        // 初始化配置文件
	        $this->upload->initialize($config);
	        if ( ! $this->upload->do_upload('news_img'))
	        {
	           echo('图片上传失败');
	           header("refresh:2;url=".site_url()."/admin/newsss/update");
	        }
	        else
	        {
	            $data['news_img'] = $this->upload->data('file_name');
	    		
	        }
	        $res = $this->db->update('news',$data,['news_id'=>$news_id]);
	        if($res){
	        	echo "<script>alert('修改成功');</script>";
	        	redirect('admin/newsss/index');
	        }else{
	        	echo('修改失败');
	           header("refresh:2;url=".site_url()."/admin/newsss/update");
	        }
		}else{


			$id = $this->input->get('id');
			$this ->load ->model('News');
			$arr = $this->News->getnewsone($id);
			$this->load->view('admin/news/update',['arr'=>$arr]);
		}
	}

	//修改案列
	public function newsupdate()
	{
		$data['news_title'] = $this->input->post('news_title');
		$data['news_content'] = $this->input->post('news_content');
		$data['cate_id'] = $this->input->post('cate_id');
		$data['add_time'] = time();
		$config['upload_path']      = './public/uploads/';
        $config['allowed_types']    = 'gif|jpg|png';
        $config['max_size']     = 100;
        $config['max_width']        = 1024;
        $config['max_height']       = 768;
        $this->upload->initialize($config);
        if ( ! $this->upload->do_upload('news_img'))
        {
           error('上传失败');die;
        }
        else
        {
            $data['news_img'] = $this->upload->data('file_name');
    
        }
        $id = $this->input->post('id');
        $res = $this->db->update('news',$data,['news_id'=>$id]);
        if($res){
        	redirect('admin/News/newslist');
        }else{
        	error('操作失败');
        }
	}



}
